package my2Cents;

import java.awt.Color;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;

import my2Cents.properties.Property;
import my2Cents.tools.AbstractPhysTool;

public class ToolMenu extends JDialog {
	private static final long serialVersionUID = 1L;
	private Vector<ToolList> toolLists = new Vector<ToolList>();
	private JTabbedPane tabbedToolListPane;
	//private JComponentList toolList;
	private JComponentList optionList;
	//private JScrollPane toolSP;
	private JScrollPane optionSP;
	private JSplitPane menuSplit;
	private int w = 700;
	private int h = 400;
	private Frame frame;
	private boolean lock = false;
	
	public ToolMenu(Frame frame) {
		this.frame = frame;
		this.setTitle("Tool Menu");
		this.setSize(w,h);
		this.setAlwaysOnTop(true);
		KeyAdapter ka = getToolMenuCloseAdpter();
		
		optionList = new JComponentList();
		optionList.setLayout(null);
		//optionList.setBackground(new Color(255,255,255));
		optionList.addKeyListener(ka);
		//optionList.setBackground(new Color(200,215,225));
		
		optionSP = new JScrollPane(optionList);
		optionSP.addKeyListener(ka);
		optionSP.setHorizontalScrollBarPolicy(
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		optionSP.setVerticalScrollBarPolicy(
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
				
		tabbedToolListPane = new JTabbedPane();
		tabbedToolListPane.addKeyListener(ka);
		
		menuSplit = new JSplitPane();
		menuSplit.setRightComponent(optionSP);
		menuSplit.setLeftComponent(tabbedToolListPane);
		menuSplit.setDividerLocation(w/2);
		menuSplit.setDividerSize(2);
		menuSplit.setEnabled(true);
		menuSplit.addKeyListener(ka);
		
		this.add(menuSplit);
		this.addKeyListener(ka);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent arg0) {
				close();
			}

			public void windowClosed(WindowEvent arg0) {
				close();
			}

			public void windowIconified(WindowEvent arg0) {
				close();
			}

			public void windowDeactivated(WindowEvent arg0) {
				close();
			}				
		});
	}
	
	public void open() {
		center();
		this.setVisible(true);
	}
	
	public void close() {
		if(lock == false) {
			this.setVisible(false);
		}
	}
	
	public boolean isOpen() {
		return this.isVisible();
	}
	
	public void lock(boolean v) {
		lock = v;
	}
	
	private ToolList newToolList(String category) {
		KeyAdapter ka = getToolMenuCloseAdpter();
		ToolList toolList = new ToolList(category);
		toolList.setKeyAdapter(ka);
		return toolList;
	}
	
	private KeyAdapter getToolMenuCloseAdpter() {
		KeyAdapter ka = new KeyAdapter() {
			public void keyReleased(KeyEvent arg0) {
				if(arg0.getKeyChar() == 'q') {
					close();
				}
			}
		};
		return ka;
	}
	
	public void center() {
		if(frame != null) {
			int f_x = frame.getLocation().x;
			int f_y = frame.getLocation().y;
			int f_w = frame.getWidth();
			int f_h = frame.getHeight();
			this.setLocation(
				(f_x + f_w/2) - w/2,
				(f_y + f_h/2) - h/2
			);
		}
	}
	
	public ToolList getListByCategory(String category) {
		for(int i=0; i<toolLists.size(); i++) {
			if(toolLists.get(i).getCategory() == category) {
				return toolLists.get(i);
			}
		}
		return null;
	}
	
	public void refreshOptionList(AbstractPhysTool tool) {
		final KeyAdapter ka = getToolMenuCloseAdpter();
		optionList.clear();
		Vector<Property> props = tool.getProperties();
		for(int i=0; i<props.size(); i++) {
			props.get(i).setCloseKeyAdapter(ka);
			optionList.add(props.get(i));
			props.get(i).validate();
		}
		optionList.requestFocus();
		optionList.resizeToFit();
		optionSP.validate();
		Sandbox.selectedTool = tool.getID();
	}
	
	public void addTool(final AbstractPhysTool tool) {
		final KeyAdapter ka = getToolMenuCloseAdpter();
		String category = tool.getCategory();
		ToolList list = getListByCategory(category);
		if(list == null) {
			list = newToolList(category);
			char Mnemonic = category.charAt(0);
			tabbedToolListPane.addTab(category,list.getScrollPane());
			tabbedToolListPane.setMnemonicAt(tabbedToolListPane.getTabCount()-1,Mnemonic);
		}
		JButton toolBtn = list.addTool(tool);
		toolBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				refreshOptionList(tool);
			}
		});
		toolLists.add(list);
	}
}
